#include "vector2d.h"

Vector2d* newVector2d(float x, float y) {
	Vector2d* v = (Vector2d*)malloc(sizeof(Vector2d));
	v->x = x;
	v->y = y;
	return v;
}

void setVector2d(Vector2d* v, float x, float y) {
	v->x = x;
	v->y = y;
}
float dotVector2d(Vector2d* v, Vector2d* v2) {
	float result = 0;
	result = v->x * v2->x + v->y * v2->y;
	return result;
}
float getLengthVector2d(Vector2d* v) {
	return (float)sqrt(v->x*v->x + v->y*v->y);
}
float getDistanceVector2d(Vector2d* v, Vector2d* v2) {
	return (float) sqrt((v2->x - v->x) * (v2->x - v->x) + (v2->y - v->y) * (v2->y - v->y));
}
Vector2d* addVector2d(Vector2d* v, Vector2d* v2) {
	Vector2d* result = newVector2d(v->x+v2->x, v->y+v2->y);
	return result;
}
Vector2d* subtractVector2d(Vector2d* v, Vector2d* v2) {
	Vector2d* result = newVector2d(v->x-v2->x, v->y-v2->y);
	return result;
}
Vector2d* multiplyVector2d(Vector2d* v, float scaleFactor) {
	Vector2d* result = newVector2d(v->x * scaleFactor, v->y * scaleFactor);
	return result;
}
void normalizeVector2d(Vector2d* v) {
	float len = getLengthVector2d(v);
	if (len != 0) {
		v->x = (v->x / len);
		v->y = (v->y / len);
	}
	else {
		v->x = 0;
		v->y = 0;
	}
}
